package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author sjy
 * @create 2022-10-20 10:37
 * 获取当前时间
 */
public class GetCurrentTime {
    /**
     * 时分秒
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取今日日期
     * @return
     */
    public static String getCurrentDay() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        Date date = new Date();
        return sdf.format(date);
    }


    /**
     * 处理时间，如果年份与今年相同，则显示月份日期，否则，显示年月日
     * @param time
     * @return
     * @throws ParseException
     */
    public static String timeConversion(String time) throws ParseException {
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 数据库中保存的年份
        int year = c.get(Calendar.YEAR);
        c.setTime(new Date());
        // 当前年份
        int nowYear = c.get(Calendar.YEAR);
        String result = null;
        if (year==nowYear){
            result = new SimpleDateFormat("M月dd日").format(date);
        }else{
            result = new SimpleDateFormat("yyyy年MM月dd日").format(date);
        }
        return result;
    }
    // 测试类
    public static void main(String[] args) throws ParseException {
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2005-12-09");
        String now = new SimpleDateFormat("M月dd日").format(date);

        System.out.println(now);
    }
}
